From 6ae88f8b1dcf3ad644dba9cb9e91bd50c99b5be5 Mon Sep 17 00:00:00 2001 From: robertl Date: Sat, 14 Jun 2008 14:44:11 +0000 Subject: [PATCH] GPX: Avoid a realloc/copy when reading large data segments that come in multiple hunks. 27x faster on pathological case of a PQ containing the first million digits of pi broken into 70 column lines. p --- gpx.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/gpx.c b/gpx.c index 20121117b..5b13010c3 100644 --- a/gpx.c +++ b/gpx.c @@ -1,7 +1,7 @@ /* Access GPX data files. - Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Robert Lipe, robertlipe@usa.net + Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Robert Lipe, robertlipe@usa.net This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -634,10 +634,9 @@ gpx_start(void *data, const XML_Char *xml_el, const XML_Char **xml_attr) /* - * FIXME: Find out why a cdatastr[0] doesn't adequately reset the - * cdata handler. + * Reset end-of-string without actually emptying/reallocing cdatastr. */ - memset(cdatastr.mem, 0, cdatastr.size); + *(char *) cdatastr.mem = 0; switch (get_tag(current_tag.mem, &passthrough)) { case tt_gpx: @@ -1162,7 +1161,7 @@ gpx_cdata(void *dta, const XML_Char *xml_el, int len) memcpy(estr, s, len); estr[len] = 0; - if (!cur_tag) + if (!cur_tag) return; if ( cur_tag->child ) { @@ -1178,11 +1177,7 @@ gpx_cdata(void *dta, const XML_Char *xml_el, int len) cdatalen = &(cur_tag->cdatalen); } estr = *cdata; - *cdata = xcalloc( *cdatalen + len + 1, 1); - if ( estr ) { - memcpy( *cdata, estr, *cdatalen); - xfree( estr ); - } + *cdata = xrealloc(*cdata, *cdatalen + len + 1); estr = *cdata + *cdatalen; memcpy( estr, s, len ); *(estr+len) = '\0'; -- 2.30.2